﻿//-----------------------------------------------------------------------
// <copyright file="ClientCommunicationManager.cs" company="FastNET Services">
//     Copyright (c) 2007-2009. All Rights Reserved.
// </copyright>
// <license>
//     GPLv2
// </license>
// <warranty>
//     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//     BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </warranty>
//-----------------------------------------------------------------------
#region Usings
using System;
using System.Collections.Generic;
using System.ServiceModel;
using FastNET.CleanAct.Client.Interfaces.Managers.Communication;
using FastNET.CleanAct.Client.Interfaces.Structures;
using FastNET.CleanAct.Common.Contracts.DataContracts.Requests;
using FastNET.CleanAct.Common.Contracts.DataContracts.Responses;
using FastNET.CleanAct.Common.Contracts.ServiceContracts;
using FastNET.CleanAct.Common.EngineModel.Managers;

#endregion

namespace FastNET.CleanAct.Client.ClientEngine.Managers.Communication
{
    /// <summary>
    /// Management class for handling all communication related processing
    /// </summary>
    internal class ClientCommunicationManager : CommunicationManagerBase, IClientCommunicationManager
    {
        #region IServerEngineContract

        /// <summary>
        /// Contacts the server using the open channel to retrieve a list of schedules to use
        /// </summary>
        /// <returns>Set of ScheduleID and the iCalendar data for the schedule or null if the information could not be retrieved</returns>
        public Dictionary<Guid, string> RetrieveSchedules()
        {
            IProcessingServerContract serverEngine = RetrieveChannel<IProcessingServerContract>();

            // If the channel is null return default
            if (serverEngine == null)
                return null;

            ResponseScheduleDataContract response;

            try
            {
                response = serverEngine.RetrieveSchedules();
                return response.Schedules;
            }
            catch (TimeoutException)
            {

            }
            catch (FaultException)
            {

            }
            catch (CommunicationException)
            {

            }

            // Default to an empty list for all failures
            return null;
        }

        /// <summary>
        /// Contacts the server using the open channel to retrieve processing inforamtion about a schedule to use
        /// </summary>
        /// <param name="scheduleID">The Schedule ID to retrieve instructions for</param>
        /// <returns>ProcessingSet with information to process by or null if the information could not be retrieved</returns>
        public ProcessingSet? RetrieveProcessingInstructions(Guid scheduleID)
        {
            IProcessingServerContract serverEngine = RetrieveChannel<IProcessingServerContract>();

            // If the channel is null return default
            if (serverEngine == null)
                return null;

            ResponseProcessingInstructionsDataContract response;
            RequestProcessingInstructionsDataContract request = new RequestProcessingInstructionsDataContract
                                                                {
                                                                    ScheduleID = scheduleID
                                                                };

            try
            {
                response = serverEngine.RetrieveProcessingInstructions(request);

                return new ProcessingSet
                       {
                           ProcessingGroup = response.ProcessingGroup,
                           XmlConfiguration = response.ProcessingConfiguration
                       };
            }
            catch (TimeoutException)
            {

            }
            catch (FaultException)
            {

            }
            catch (CommunicationException)
            {

            }

            // Default to an empty list for all failures
            return null;
        }

        #endregion

        #region Protected Overrides

        /// <summary>
        /// Called to allow the inherited class to initialize the instance of the factory for the requested channel type
        /// </summary>
        /// <param name="channelType">Type of the channel the factory should be for</param>
        /// <returns>ChannelFactory of IChannel that was created</returns>
        protected override ChannelFactory CreateFactoryInstance(Type channelType)
        {
            if (channelType == typeof(IProcessingServerContract))
            {
                return new DuplexChannelFactory<IClientEngineContract>(this);
            }

            return null;
        }

        /// <summary>
        /// Called to allow the inherited class to initialize the instance of the ServiceHost for the requested host type
        /// </summary>
        /// <param name="hostType">Type of the host should be for</param>
        /// <returns>ServiceHost that was created</returns>
        protected override ServiceHost CreateHostInstance(Type hostType)
        {
            return null;
        }

        /// <summary>
        /// Gets a list of all channel types this communication manager is managing
        /// </summary>
        /// <returns>List of Type for all supported channels</returns>
        protected override List<Type> GetChannelTypes()
        {
            return new List<Type> { typeof(IProcessingServerContract) };
        }

        /// <summary>
        /// Gets a list of all host types this communication manager is managing
        /// </summary>
        /// <returns>List of Type for all supported host</returns>
        protected override List<Type> GetHostTypes()
        {
            return new List<Type>();
        }

        #endregion
    }
}
